package com.application.model.universidades.universidadesorg.retrieve;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.application.model.RetrivierWeb;
import com.application.model.universidades.universidadesorg.manager.ManagerUniversidadesOrg;

public class ManagerDetalle extends RetrivierWeb{

	private ManagerUniversidadesOrg informacion; 
	
	public ManagerDetalle(ManagerUniversidadesOrg informacion) {
		this.informacion = informacion;
	}

	
	@Override
	public void processContent(String data, String parentName) {
		Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        String      nombre = "";
        String      nivel = "";
        String      tipo = "";
        String      direccion = "";
        String      codigoPostal = "";
        String      telefono = "";
        String      email = "";
        String      url = "";
        regex = "</a> - (.*?)<br>(.*?)<b>PUBLICA/PRIVADA</b>:(.*?)<br><b>Dirección</b>:(.*?)<br><b>Código Postal</b>:(.*?)<br><b>Teléfono</b>:(.*?)<br><b>E-Mail</b>:(.*?)<br><b>Dirección en internet</b>:(.*?)<br>";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
            nombre = getNombre(matcher.group(1));
            nivel = getNivel(matcher.group(1));
            tipo = getTipo(matcher.group(3));
            direccion = getDireccion(matcher.group(4));
            codigoPostal = getCodigoPostal(matcher.group(5));
            telefono = getTelefono(matcher.group(6));
            email = getEmail(matcher.group(7));
            url = getUrl(matcher.group(8));
            logger.info("detalle : nombre "+nombre+" tipo "+tipo+" direccion "+direccion+" codigo postal "+codigoPostal+" telefono "+telefono+" email "+email+" url "+url);
        }
	}

	


	private String getUrl(String data) {
		Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
		String 		value = "";
		
		if(!data.isEmpty()){
	        regex = "<a href=\"(.*?)\"";
	        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
	        matcher = pattern.matcher(data);
	        
	        while (matcher.find()) {
	        	value = matcher.group(1);            
	            value = value.trim();
	        }
		}
		return value;
	}

	private String getNivel(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.trim();
			if(value.equalsIgnoreCase("Centro")){
				value="Cursos";
			}
		}
		return value;
	}
	private String getEmail(String data) {
		String value = "";
		if(!data.isEmpty()){
//			value = data.replace("", "");
			value = data.trim();
		}
		return value;
	}


	private String getTelefono(String data) {
		String value = "";
		if(!data.isEmpty()){
//			value = data.replace("", "");
			value = data.trim();
		}
		return value;
	}


	private String getCodigoPostal(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.replace("(", "");
			value = value.replace(")", "");
			value = value.trim();
		}
		return value;
	}


	private String getDireccion(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.replace("(Capital Federal.)", "");
			value = value.trim();
		}
		return value;
	}


	private String getTipo(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.replace(".", "");
			value = value.trim();
		}
		return value;
	}


	private String getNombre(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.replace(".", "");
			value = value.replaceAll("\\(.*\\)","");
			value = value.trim();
		}
		return value;
	}


	@Override
	public void retrieve() {
		// TODO Auto-generated method stub
		
	}

}
